home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / presto / prest_04.lha / src / spinlock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-08  |  1.8 KB  |  110 lines

  1. //
  2. // spinlock.c
  3. //    non-inlined functions for spinlocks
  4. //
  5.  
  6. #include <stream.h>
  7. #include "presto.h"
  8.  
  9. void
  10. Spinlock::print(ostream& s)
  11. {
  12.     s << form("(Spinlock)this=0x%x, sl_lock=0x%x", this, this->sl_lock);
  13. }
  14.  
  15.  
  16. #ifdef vax
  17.  
  18. //
  19. // These are the functions that eventually get called for operations on
  20. // spinlocks on a VAX.
  21. //
  22.  
  23. void
  24. s_init_lock(slock_t* l)
  25. {
  26.      *l  = 0;
  27. }
  28.  
  29. /*
  30.  * s_lock, s_unlock and s_clock are defined in asm code in vax_lock.s
  31.  * XXX asm versions of s_lock and s_unlock don't return a value, should
  32.  * be void.
  33.  *
  34.  * s_clock acquires the spinlock iff it is free, else returns immediately
  35.  * without spinning.
  36.  *
  37.  * The asm versions of these functions assume that a lock is a longword.
  38.  * The low-order bit of the word is used as the lock bit.  The other bits
  39.  * are unused.
  40.  */
  41.  
  42. /*
  43.  
  44. int
  45. *s_lock(slock_t *l)
  46. {
  47.     while (*l)
  48.      ;
  49.     return(*l++);
  50. }
  51.  
  52.  
  53. int
  54. s_unlock(slock_t* l)
  55. {
  56.      *l = 0;
  57.      return(1);
  58. }
  59.  
  60.  
  61. int
  62. s_clock(slock_t* l)
  63. {
  64.      return((*l) ? 0 : (*l)++);
  65. }
  66.  
  67. */
  68.  
  69. #endif vax
  70. #ifdef sun
  71. #ifdef DO_SPINLOCK_INLINE
  72.         //
  73.         // s_lock, s_unlock and s_clock are defined in sun_lock.s
  74.         //
  75.         void s_init_lock(slock_t* l) {
  76.         *l  = 0; 
  77.     }
  78.  
  79.         void s_lock(slock_t *l) { 
  80.         while (*l) ; *l =  1; 
  81.     }
  82.         void s_unlock(slock_t* l) { 
  83.         *l = 0; 
  84.     }
  85.         int s_clock(slock_t* l) { 
  86.         return((*l) ? 0 : (*l)++);
  87.     }
  88. #else
  89.         void Spinlock::~Spinlock() {
  90.                 if (sl_lock)
  91.                         unlock();
  92.         }
  93.  
  94.         void Spinlock::unlock() {
  95.                 (void) S_UNLOCK(&sl_lock);
  96. #   ifndef NO_PREEMPT
  97.                 thisthread->releasingspinlock();
  98. #   endif  NO_PREEMPT
  99.         }
  100.  
  101.         void Spinlock::lock() {
  102. #   ifndef NO_PREEMPT
  103.                 thisthread->holdingspinlock();
  104.  
  105. #   endif NO_PREEMPT
  106.                 (void) S_LOCK(&sl_lock);
  107.         }
  108. #endif DO_SPINLOCK_INLINE
  109. #endif sun
  110.